
//jquery datatable 对象
var searchTable;

$(function(){
	
	// 后面构建btn 代码
	var btnModel = '    \
		{{#each func}}\
		<button type="button" class="user-button user-{{this.type}} btn-sm" onclick="{{this.fn}}">{{this.name}}</button>\
		{{/each}}';
	var template = Handlebars.compile(btnModel);
	// datatables 
	searchTable = $("#searchTable").DataTable({
		"ordering": false,// 排序
		"serverSide": true,// 开启服务器模式
		"scrollX": true,// 横向滚动
		ajax: {
			"type": "POST",
			"url":'search4PageList.action',//请求路径
			"contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
			"dataType":'json',
			"data":function(d){
			// 查询参数
			<#list beanList as column>
				<#--查询条件,如查询条件为空直接过滤掉否则后台BEAN中存在INT类型时会出现类型转换错误-->
				<#if column.isSelect='1'>
					var ${column.newColumnName}=$('#${column.newColumnName}').val();
					if(${column.newColumnName} !='' && ${column.newColumnName} != null){
						d.${column.newColumnName}=${column.newColumnName};
					}
				</#if>
			</#list>
				return d;
			}
		},
		columns: [// 对应列
			<#list beanList as column>
				<#--台帐字段-->
				<#if column.isList='1'>
					{"data": "${column.newColumnName}","title":"${column.displayComment!'名称为空'}",className: "text-center"},
				</#if>
			</#list>
					{"data": null,"title":"操作",className: "text-center"}
		],
		"columnDefs": [
			{// 所有列默认值
				"targets": "_all",
				"defaultContent": ''
			},
			//{// 隐藏第一列
			//	"targets": 0,
			//	"visible": false 
			//},
			{// 最后一列添加按钮
				targets: -1,
				render: function (a, b, c, d) {
				var btnArray = new Array();
					<#--主键字段，此条件在controller中组合-->
					btnArray.push({"name": "修改", "fn": "showEditPage(${bean.asmKeyCom})"});
					btnArray.push({"name": "删除", "fn": "del(${bean.asmKeyCom})", "type": "btn-n"});
				context = {
						func:btnArray
				}
				var html = template(context);
				return html;
			}
		}]
		,"dom": 'rt<"pull-left mt5"i><"pull-right mt5"p><"clear">' //生成样式
	});
});

//重置
function searchReset(){
	$("input").val("");
}

// 查询
function search4Page(){
	searchTable.ajax.reload();
}

//删除
<#--主键字段，此条件在controller中组合-->
function del(${bean.asmKeyList}){
	if(confirm('确定删除此条数据?')){
		$.ajax({
			url:"deleteByPrimaryKey.action",
			data:{${bean.asmKeyCol}},
			dataType:"text",
			type:"POST",
			success:function(data){
				if('1'==data){
					alert("删除成功！");
				}else{
					alert("删除失败！");
				}
				searchTable.draw(false);
			},
			error:function(e){
				alert("删除信息时异常："+e);
			}
		})
	}
}

//新增
function showAddPage(){
	window.open('showAddPage.action','',"height=450px,width=800px,toolbar=no,resizable=yes,location=no,top=150,left=200");
}

//修改
function showEditPage(${bean.asmKeyList}){
	var url = 'selectByPrimaryKey.action?${bean.asmKeyUrl};
	window.open(url,'',"height=450px,width=800px,toolbar=no,resizable=yes,location=no,top=150,left=200");
}
